package com.zys.http.ui.tab.panel;

import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBPanel;
import com.intellij.util.ui.JBUI;
import com.zys.http.constant.HttpEnum;
import com.zys.http.constant.UIConstant;
import com.zys.http.entity.ReqHistory;
import com.zys.http.entity.param.ParamProperty;
import com.zys.http.ui.table.ParameterTable;
import lombok.Getter;

import java.awt.*;
import java.util.Map;

/**
 * @author zys
 * @since 2025-02-19
 */
@Getter
public class RequestParamPanel extends JBPanel<RequestParamPanel> implements TabOperation {
    private final ParameterTable parameterTable;

    public RequestParamPanel(Project project) {
        super(new BorderLayout(0, 0));
        this.parameterTable = new ParameterTable(project);
        this.parameterTable.getToolbar().getComponent().setBorder(JBUI.Borders.customLineBottom(UIConstant.BORDER_COLOR));
        this.add(parameterTable);
    }

    @Override
    public void reset() {
        this.parameterTable.reloadTableModel();
    }

    @Override
    public void execute(HttpEnum.HttpMethod httpMethod, HttpEnum.ContentType contentType, String param, Map<String, ParamProperty> paramPropertyMap) {
        ParamProperty v = paramPropertyMap.get(param);
        HttpEnum.ParamUsage usage = v.getParamUsage();
        switch (usage) {
            case PATH, URL -> this.parameterTable.getTableModel().addRow(new String[]{param, v.getDefaultValue() + ""});
            default -> {
            }
        }
    }

    @Override
    public void recover(ReqHistory history) {
        this.parameterTable.setModel(history.getParams());
    }
}
